<?php

namespace App\Admin\Controllers;

use Encore\Admin\Layout\Content;
use Carbon\Carbon;
use App\Models\GcCategory;
use App\Models\User;
use App\Models\GcUserImage;
use App\Models\GcGroup;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;
/*use App\Admin\Actions\CollectionAction\MaterialsImages;
use App\Admin\Actions\CollectionAction\MaterialsImagesUpload;*/

class GcUserImagesController extends AdminController
{
    protected $title = '分配列表';
    
    protected function grid()
    {
        $grid = new Grid(new GcUserImage());
        $grid->model()->orderByDesc('id');
	
	    $grid->disableExport();
	    
	    $grid->column('id', 'ID');
        $grid->column('gc_group_id', '分组')->display(function () {
            return isset($this->gcGroup) ? "{$this->gcGroup->id}: {$this->gcGroup->name}" : '';
         });
        $grid->column('gc_category_id', '分类')->display(function () {
            return isset($this->gcCategroy) ? "{$this->gcCategroy->id}: {$this->gcCategroy->name}" : '';
         });

         $grid->column('gc_type', '渠道')->display(function ($value) {
            return config("site.gcType.$value");
        });

        $grid->column('user_id', '绑定用户')->display(function () {
            return $this->user ? $this->user->nameText(true) : '';
        });

        $grid->column('title_main', '分配文案标题');

         $grid->column('is_used', '是否已下载')->display(function ($value) {
             return $value ? _true() : _false();
        });

        

       $grid->disableCreation();
       
       $this->gridFilter($grid);
        $this->gridActions($grid);
      
        return $grid;
    }
    
    protected function gridActions(Grid $grid)
    {
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        
        });
    }


    protected function detail($id)
    {
        $show = new Show(GcUserImage::findOrFail($id));

        return $show;
    }

     protected function gridFilter(Grid $grid)
    {
        $grid->filter(function (Grid\Filter $filter) {
            $filter->expand();
            $filter->column(1/2, function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('user_id', '会员 ID 查询');
            });
        });
    }

    
      protected function form()
    {
        $form = new Form(new GcUserImage());
        $form->select('gc_group_id','分组')->options(Gcgroup::get()->pluck('name', 'id'));
        $form->select('gc_category_id','分类')->options(GcCategory::get()->pluck('name', 'id'));
        $form->select('user_id','用户')->options(User::get()->pluck('name', 'id'));
         $form->select('gc_type','渠道')->options(config("site.gcType"));
        $form->multipleImage('images', '图片')->removable()->sortable();

        $form->text('title_main', '分配文案标题');
        $form->simditor('content_main', '分配文案内容');
        //$this->formSaved($form);

        return $form;
    }

  	/*protected function formSaved(Form $form)
	{
		$form->saved(function () use ($form) {
			dd($form);
			//$product = $this->product($form->model()->id) ?? null;
			//app(MakeProductThumbnail::class, ['product' => $product])->make();
		});
	}*/
  
}
